package com.cnhis.cloudhealth.clinical.hosdoctor.executionlist.bo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo.DoctorparameterBo;
import com.cnhis.cloudhealth.clinical.hosdoctor.executionlist.dao.ExecutionlistDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.bo.HosSendAdviceBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.service.IHosSendAdvice;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;
import org.apache.commons.collections.MapUtils;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ExecutionlistBo {
	@Resource
	private ExecutionlistDao executionlistDao;
	@Autowired
	private DoctorparameterBo DoctorparameterBo;
	@Autowired
    private HosSendAdviceBo HosSendAdviceBo;
	private List<Map<String,String>> changeToString(List<?> list){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		for(Object o:list){
			if(o instanceof Map){
				Map<String,Object> pmap = (Map<String,Object>)o;
				Map<String,String> smap = new HashMap<String,String>();
				Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<String, Object> entry = it.next();
					String str = String.valueOf(entry.getValue());
					smap.put(entry.getKey(),str);
				}
				resultList.add(smap);
			}
		}
		return resultList;
	}

	private ModelVo packModelVo(String nid, Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = executionlistDao.queryForList(nid,map);
			list = changeToString(list);
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("数据操作异常！");
			e.printStackTrace();
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}
	public ModelVo selectExcutionlist(Map<String,Object> map) throws Exception{
		ModelVo mo = new ModelVo();
		mo =  packModelVo("Executionlist.7276",map);
		String type = checklisbill(mo);
		if(type.equals("true")){
			return packModelVo("Executionlist.7276",map);
		}else{
			return mo;
		}		
	}
	public ModelVo selectExcutionlist2(Map<String,Object> map) throws Exception{
		ModelVo mo = new ModelVo();
		mo = packModelVo("Executionlist.7277",map);
		String type = checklisbill(mo);
		if(type.equals("true")){
			return packModelVo("Executionlist.7277",map);
		}else{
			return mo;
		}
	}
	public ModelVo selectPatientexeList(Map<String,Object> map){
		ModelVo mo = new ModelVo();
		mo = packModelVo("Executionlist.7720",map);
		return mo;
	}
	//检验是否有条码号
	public String checklisbill(ModelVo mo) throws Exception{
		if(DoctorparameterBo.getSysValue(100,105001,394).equals("1")){
			List<?> list = mo.getList();
			int tof = 0;
			String lXml = "{\"VAF1\":[";
			Long lBCE01 = 0l; //员工ID
			Long lBCE02 = 0l; //当前操作员编号
			String lBCE03 = "";  //当前操作员姓名
			if(list!=null&&list.size()>0){
				for(int i=0;i<list.size();i++){
					Map map = (Map) list.get(i);
					String vbi21 = (String) map.get("vbi21");
					int vaf11 = MapUtils.getInteger(map,"vaf11",0);
					if(vbi21.equals("") && vaf11==2){
						Long vaf01 = MapUtils.getLong(map,"vaf01",0l);
						Long vaa01 = MapUtils.getLong(map,"vaa01",0l);
						lBCE01 = MapUtils.getLong(map,"bce01"); //员工ID
						lBCE02 = MapUtils.getLong(map,"bce02"); //当前操作员编号
						lBCE03 = MapUtils.getString(map,"bce03");  //当前操作员姓名
						if(tof>0){
        					lXml+=",";
        				}
        				lXml+="{\"vaf01\":\""+vaf01+"\",\"vaa01\":\""+vaa01+"\",\"vaf11\":"+vaf11+"}";
        				tof++;						
					}
				}
				lXml+="]}";
				if(tof>0){
					Map map1 = new HashMap();
					map1.put("lXml", lXml);
					map1.put("lBCE01", lBCE01);
					map1.put("lBCE02", lBCE02);
					map1.put("lBCE03", lBCE03);
					map1.put("lACF01", 2);
					HosSendAdviceBo.nurseLisBillsPrintAuto(map1);
					return "true";
				}
			}			
		}	
		return "false";
	}
	public ModelVo lisBillsPrint(Map<String,Object> map) throws Exception{
		String lVAFid = MapUtils.getString(map,"lVAFid","");
		if(StringUtils.isBlank(lVAFid)){
			return new ModelVo(StaticKeys.OH_FALSE, "医嘱记录参数为空，请检查参数！");
		}
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(lVAFid);
		if(lVAFidList==null|| lVAFidList.size()==0){
			return new ModelVo(StaticKeys.OH_FALSE, "参数解析有误，请检查！");
		}
		map.put("lVAFidList",lVAFidList);
		Long vafcn = (Long)executionlistDao.queryToObject("Executionlist.7276",map);
		Integer VAE44 = MapUtils.getInteger(map,"VAE44");
		Integer lACF01 = MapUtils.getInteger(map,"lACF01");
		if(lACF01==2) {
			Integer aPart = 1;
			if (vafcn > 0 && VAE44 == 5) {
				aPart = 2;
			}
			map.put("aPart", aPart);
			String lBDA01 = MapUtils.getString(map, "lBDA01");
			if ("L".equals(lBDA01)) {
				executionlistDao.insert("Executionlist.insertTempBEL_L2", map);
			} else if ("E".equals(lBDA01)) {
				executionlistDao.insert("Executionlist.insertTempBEL_E2", map);
			} else if ("S".equals(lBDA01) || "Z".equals(lBDA01)) {
				executionlistDao.insert("Executionlist.insertTempBEL_SZ2", map);
			}
		}else if(lACF01==1){
			executionlistDao.insert("Executionlist.insertTempBEL_L", map);
		}
		return null;
	}

	public ModelVo selectOrderprintinfo(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.2108",map);
	}
	public ModelVo selectOrderdetailprint(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.2109",map);
	}

	public ModelVo selectOrdersissued(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7687",map);
	}
	public ModelVo selectNurseexeamount(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7739",map);
	}
	public ModelVo selectApplicationform(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7683",map);
	}
	public ModelVo selectApplicationformdetail(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7684",map);
	}
	public ModelVo selectHistorysummary(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.6908",map);
	}

	public ModelVo selectAdditionalcontent(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7735",map);
	}

	public ModelVo selectOperationarrange(Map<String,Object> map){
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lXml"));
		map.put("vafidList",lVAFidList);
		return packModelVo("Executionlist.7685",map);
	}
	public ModelVo nurseLisBillsPrintOk(Map<String,Object> map)throws Exception{
		//将Json数据装换为list
		List<Map<String,Object>> lVAFidList = JsonUtil.getList(MapUtils.getString(map,"lVAFid"));
		map.put("vafidList",lVAFidList);
		executionlistDao.update("Executionlist.Nurse_Lis_Bills_Print_Ok",map);
		return new ModelVo(StaticKeys.OH_SUCCESS, "更新成功！");
	}
	public ModelVo selectProjectnamelist(Map<String,Object> map){return packModelVo("Executionlist.projectnamelist",map);}
	public ModelVo selectThezoneexelist(Map<String,Object> map)throws Exception{
		//将Json数据装换为map
		Map<String,Object> lxmlMap = JsonUtil.getMap(MapUtils.getString(map,"lXml"));
		List<Map<String,Object>> vaeList = (List<Map<String,Object>>)lxmlMap.get("vae");
		List<Map<String,Object>> bbxList = (List<Map<String,Object>>)lxmlMap.get("bbx");
		map.put("vaeList",vaeList);
		map.put("bbxList",bbxList);
		return packModelVo("Executionlist.thezoneexelist",map);
	}
	public ModelVo selectIsOprationorder(Map<String,Object> map){return packModelVo("Executionlist.7722",map);}
	public ModelVo printBeforeHospitalization(Map<String,Object> map) throws Exception{
		Long lVaf01 = MapUtils.getLong(map,"lVaf01");
		Date adate = DateUtils.parseDate("9999-12-30", new String[]{"yyyy-MM-dd"});
		map.put("adate",adate);
		if(lVaf01>0) {
			Map<String, Object> vafMap = (Map<String, Object>) executionlistDao.queryToObject("Executionlist.printbefore_001", map);
			Date vaf36 = (Date) vafMap.get("vaf36");
			Long vaf06 = MapUtils.getLong(vafMap, "vaf06");
			System.out.println("结果1" + vaf36 + "," + vaf06);
			map.put("VAF36b", vaf36);
			map.put("VAA07", vaf06);
			Date eVAF36 = (Date) executionlistDao.queryToObject("Executionlist.printbefore_002", map);
			if (eVAF36 == null) {
				eVAF36 = adate;
			}
			map.put("VAF36e", eVAF36);
			return packModelVo("Executionlist.printbefore_003",map);
		}else{
			return packModelVo("Executionlist.printbefore_004",map);
		}
	}
	public ModelVo labelprinting(Map<String,Object> map) throws Exception{
		Map<String, Object> vaeMap = (Map<String, Object>) executionlistDao.queryToObject("Executionlist.7719_001", map);
		String VAA04 = MapUtils.getString(vaeMap,"vaa04");
		String BCQ04B = MapUtils.getString(vaeMap,"bcq04b");
		map.put("VAA04",VAA04);
		map.put("BCQ04B",BCQ04B);
		return packModelVo("Executionlist.7719_002",map);
	}

}
